<?php

class IndexController extends Zend_Controller_Action
{

   public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        // action body
    }
    
    public function nosotrosAction()
    {
        // action body
    }
    
    public function productosAction()
    {
		$menu = new Nutri_Model_Menu();
		$resultMenu = $menu->fetchAll();
    	
    	if(empty($resultMenu)) {
    		$this->view->listMenues = NULL;
    	} else {
	    	$menuArray = array();
	    	foreach ($resultMenu as $itemMenu) {
	    		$itemMenuArray = array();
	    		$itemMenuArray['id'] = $itemMenu->getId();
	    		$itemMenuArray['Titulo'] = $itemMenu->getTitle();
	    		$itemMenuArray['Descripcion'] = $itemMenu->getDescription();
	    		$itemMenuArray['Fecha_de_fin'] = $itemMenu->getEnddate();
				$itemMenuArray['Precio'] = $itemMenu->getPrice();
				$itemMenuArray['Color'] = $itemMenu->getColor();
	    		
	    		$menuArray[] = $itemMenuArray;
	    	}
	    	
	    	$this->view->listMenues = $menuArray;
    	}	
    }
    
    public function novedadesAction()
    {
		$news = new Nutri_Model_News();
		$resultNews = $news->fetchAll(
			$news->select()
				->order('creationday DESC')
		);
    	if(empty($resultNews)) {
    		$this->view->h1 = NULL;
    	} else {
	    	$newsArray = array();
	    	foreach ($resultNews as $itemNews) {
	    		$itemNewsArray = array();
	    		$itemNewsArray['id'] = $itemNews->getId();
	    		$itemNewsArray['Titulo'] = $itemNews->getTitle();
				$itemNewsArray['Estracto'] = substr($itemNews->getText(), 0, 100) . '...';
				$itemNewsArray['Date'] = Nutri_Date_ShowDate::displayDateNews($itemNews->getCreationday());
	    		
	    		$newsArray[] = $itemNewsArray;
	    	}
	    	$this->view->listNews = $newsArray;
    	}
    }
	
	public function novedadesampliadaAction()
    {
        $id = $this->getRequest()->getParam('id');	
        $newSelected = new Nutri_Model_News();
		$resultNewsSelected = $newSelected->fetchRow($newSelected->select()->where('id = ?', $id));
		
    	if(empty($resultNewsSelected)) {
    		$this->view->detailNew = NULL;
    	} else {
    		$dataComplete = $resultNewsSelected->toArray();
			$dataComplete['creationday'] = Nutri_Date_ShowDate::displayDateNews($dataComplete['creationday']);
	    	$this->view->detailNew = $dataComplete;
    	}
    }
    
    public function registroAction()
    {
    	$this->view->h1 = 'Registro';
        //$id = $this->getRequest()->getParam('id');
        $formRegister = new Nutri_Form_User();
		
		$request = $this->getRequest();
		
		if ($request->isPost()) {
    		if ($formRegister->isValid($request->getPost())) {
                $news = new Nutri_Model_User($formRegister->getValues());
				$news->save();
				$body = $formRegister->getValues();
    			$message = 'Nombre: ' . $body['fullname'] . '<br/>' .
    				'E-mail: ' . $body['email'] . '<br/>' .
    				'Telefono: ' . $body['tel'] . '<br/>' .
    				'Direccion: ' . $body['addres'];
    			$mail = new Zend_Mail();
			    $mail->setSubject('Nutrisimo - Registro de Usuario')
			         ->setBodyHtml(
			    	     '<h2>Nuevo Usuario Registrado</h2><br/>' . $message
			         )
			         ->addTo($mail);
					
				$emailSender = new Nutri_Email_EmailSender();
				$emailSender->sendEmail($mail);
		    	$formRegister = new Nutri_Form_User();
				$this->view->updateData = "Usuario Registrado correctamente.";                
            }
			
        }
        
		$this->view->formRegister = $formRegister;
    }
    
    public function contactoAction()
    {
      	$this->view->h1 = 'Contactanos';
    	
		$id = $this->getRequest()->getParam('id');
        $formContact = new Nutri_Form_Contacto(array('id' => $id));
		$config = Zend_Registry::get('config');
		$request = $this->getRequest();
		
		if ($request->isPost()) {
    		if (is_null($id) && $formContact->isValid($request->getPost())) {
    			$body = $formContact->getValues();
    			$message = 'Nombre: ' . $body['completename'] . '<br/>' .
    				'E-mail: ' . $body['email'] . '<br/>' .
    				'Telefono: ' . $body['telephone'] . '<br/>' .
    				'Descripcion: ' . $body['comment'];
    			$mail = new Zend_Mail();
			    $mail->setSubject('Nutrisimo - Contacto via web')
			         ->setBodyHtml(
			    	     '<h2>Mensaje desde el sitio web</h2><br/>' . $message
			         )
			         ->addTo($mail);
					
				$emailSender = new Nutri_Email_EmailSender();
				$emailSender->sendEmail($mail);
		    	//$this->_redirect('/admin/productos');          
            }
        }
			
		$this->view->formContact = $formContact;
    }
}